package com.newbingo.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IBackupDao;
import com.newbingo.entity.Backup;
import com.newbingo.util.Pager;

public class BackupDao extends HibernateDaoSupport implements IBackupDao {
	private static final Log log = LogFactory.getLog(BackupDao.class);

	/*
	 * 删除(non-Javadoc)
	 * @see com.newbingo.dao.imple.IBackupDao#delete(com.newbingo.entity.Backup)
	 */
	public void delete(Backup backup) {
		log.debug("deleting Backup instance");
		try {
			Backup instance=this.findById(backup.getId());
			getSession().delete(instance);
		} catch (RuntimeException re) {
			log.error("delete Backup instance failed",re);
			throw re;
		}

	}

	/*
	 * 根据ID查询(non-Javadoc)
	 * @see com.newbingo.dao.imple.IBackupDao#findById(java.lang.Integer)
	 */
	public Backup findById(Integer Id) {
        log.debug("finding Backup instance with Id:"+Id);
        try {
			Backup instance=(Backup)getSession().get("com.newbingo.entity.Backup",Id);
			return instance;
		} catch (RuntimeException re) {
			log.error("find Backup Instance failed",re);
			throw re;
		}
	}

	/*
	 * 分页查询(non-Javadoc)
	 * @see com.newbingo.dao.imple.IBackupDao#findByPage(int, int)
	 */
	public Pager findByPage(int start, int limit) {
        Pager pager=new Pager();
        Criteria cri=getSession().createCriteria(Backup.class);
        cri.setProjection(Projections.rowCount());
        pager.setTotalSize(((Number)cri.uniqueResult()).intValue());
        cri.setProjection(null);
        cri.setResultTransformer(Criteria.ROOT_ENTITY);
        cri.setFirstResult(start);
        cri.setMaxResults(limit);
        pager.setData(cri.list());
		return pager;
	}

	/*
	 * 保存(non-Javadoc)
	 * @see com.newbingo.dao.imple.IBackupDao#save(com.newbingo.entity.Backup)
	 */
	public void save(Backup backup) {
        log.debug("saving Backup instance");
        try {
			getSession().save(backup);
		} catch (RuntimeException re) {
			log.error("save Backup instance failed",re);
			throw re;
		}
	}

}
